package com.xywy.find.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.xywy.R;
import com.xywy.find.JkgcItemActivity;
import com.xywy.find.bean.NewJkgcBig;
import com.xywy.find.bean.NewJkgcItem;

/**
 * 健康广场的list适配器
 * 
 * @author yangyu
 */
public class NewJkgcAdapter extends BaseAdapter {

	private Activity mContext;
	private List<NewJkgcItem> mLists = new ArrayList<NewJkgcItem>(); // item数据集合
	private NewJkgcBig mBigData; // header大图片的数据
	private String mTarget = null;// 请求数据需要使用，表示当前在哪个栏目下
	private	String 	mTitle;//当前的标题

	final int VIEWTYPE = 2; // 两种布局
	final int VIEW_HEAD = 0; // 第一种头部布局
	final int VIEW_ITEM = 1; // 第二种条目布局

	public NewJkgcAdapter(Activity context) {
		this.mContext = context;
		initImageLoader();
	}

	/**
	 * 设置最新的数据
	 * 
	 * @param lists
	 */
	public void setListData(List<NewJkgcItem> lists) {
		this.mLists = lists;

	}

	/**
	 * 设置第一个图片的显示数据；
	 */
	public void setBigData(NewJkgcBig bigData) {
		this.mBigData = bigData;
	}

	/**
	 * 添加数据
	 * 
	 * @param lists
	 */
	public void addListData(List<NewJkgcItem> lists) {
		mLists.addAll(lists);
	}

	/**
	 * 设置当前条目的target
	 * 
	 * @param target
	 */
	public void setCurrentTarget(String target) {
		this.mTarget = target;
	}

	/**
	 *  设置当前的标题
	 * @param currentTitle
	 */
	public void setCurrentTitle(String currentTitle) {
		this.mTitle = currentTitle;
		
	}

	/**
	 * 清理当前列表数据；
	 */
	public void clearListData() {
		if (mLists != null) {
			mLists.clear();
		}
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return VIEW_HEAD;
		} else {
			return VIEW_ITEM;
		}

	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return VIEWTYPE;
	}

	@Override
	public int getCount() {
		return mLists.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mLists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View view, ViewGroup arg2) {
		int type = getItemViewType(arg0);
		ViewHolderAD vha = null;
		ViewHolderItems vhi = null;

		if (view == null) {

			switch (type) {
			case VIEW_HEAD:
				vha = new ViewHolderAD();
				view = View.inflate(mContext, R.layout.find_jkgc_new_header,
						null);
				vha.rl_header = (RelativeLayout) view
						.findViewById(R.id.rl_header);
				vha.tv_Ad_description = (TextView) view
						.findViewById(R.id.tv_Ad_description);
				vha.iv_Ad = (ImageView) view.findViewById(R.id.iv_Ad);
				view.setTag(vha);
				break;

			case VIEW_ITEM:
				vhi = new ViewHolderItems();
				view = View
						.inflate(mContext, R.layout.find_jkgc_new_item, null);
				vhi.ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
				vhi.tv_title = (TextView) view.findViewById(R.id.tv_title);
				vhi.tv_desc = (TextView) view.findViewById(R.id.tv_desc);
				vhi.iv_item = (ImageView) view.findViewById(R.id.iv_item);
				vhi.iv_new_news = (ImageView) view
						.findViewById(R.id.iv_new_news);
				view.setTag(vhi);
				break;
			}

		} else {
			switch (type) {
			case VIEW_HEAD:
				vha = (ViewHolderAD) view.getTag();
				break;
			case VIEW_ITEM:
				vhi = (ViewHolderItems) view.getTag();
				break;
			}

		}

		// ---------------

		switch (type) {
		case VIEW_HEAD:
			if (null == mBigData || mBigData.toString() == null
					|| mBigData.toString().equals("")) {
				vha.rl_header.setVisibility(View.GONE);
			} else {
				vha.tv_Ad_description.setVisibility(View.VISIBLE);
				circleImageView(mBigData.getLitpic(), vha.iv_Ad);
				vha.tv_Ad_description.setText(mBigData.getTitle());

				vha.rl_header.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO： 此处跳转
						Intent intent = new Intent(mContext,
								JkgcItemActivity.class);
						intent.putExtra(JkgcItemActivity.TARGET, mTarget);
						intent.putExtra(JkgcItemActivity.TITLE, mTitle);
						intent.putExtra(JkgcItemActivity.ID, mBigData.getId()
								+ "");
						
						mContext.startActivity(intent);
					}
				});
			}
			break;
		case VIEW_ITEM:
			final NewJkgcItem item = mLists.get(arg0 - 1);
			vhi.tv_title.setText(item.getTitle());
			vhi.tv_desc.setText(item.getBody());
			circleImageView(item.getLitpic(), vhi.iv_item);
			vhi.iv_new_news.setVisibility(View.GONE);
			vhi.ll_item.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
//					Toast.makeText(mContext,
//							"点击item" + mLists.get(arg0 - 1).getId(),
//							Toast.LENGTH_LONG).show();
					Intent intent = new Intent(mContext, JkgcItemActivity.class);
//					intent.putExtra(JkgcItemActivity.TARGET, mTarget);
					intent.putExtra(JkgcItemActivity.TARGET, mTarget);
					intent.putExtra(JkgcItemActivity.ID, mLists.get(arg0 - 1)
							.getId() + "");
					intent.putExtra(JkgcItemActivity.TITLE, mTitle);
					mContext.startActivity(intent);
				}
			});
			break;
		}

		return view;
	}

	static class ViewHolderItems {
		private LinearLayout ll_item;
		private TextView tv_title;
		private TextView tv_desc;
		private ImageView iv_item;
		private ImageView iv_new_news;

	}

	static class ViewHolderAD {
		private RelativeLayout rl_header;
		private TextView tv_Ad_description;
		private ImageView iv_Ad;
	}

	// ====ImageLoader start====
	public static final List<String> displayedImages = Collections
			.synchronizedList(new LinkedList<String>());
	// ImageLoaderOptions
	private DisplayImageOptions options;

	// ImageLoader
	protected ImageLoader imageLoader = ImageLoader.getInstance();

	private void initImageLoader() {
		options = new DisplayImageOptions.Builder()
				// .showImageForEmptyUri(R.drawable.ic_error) // resource or
				// drawable
				.showImageOnFail(R.drawable.imageloader_error)
				// resource or drawable
				.showStubImage(R.drawable.imageloader_default)
				.resetViewBeforeLoading(false)
				// default
				.cacheInMemory(true)
				.cacheOnDisc(true)
				// 精准
				.imageScaleType(ImageScaleType.EXACTLY)
				// ARGB_8888 每个像素要4字节~带透明度的颜色, 默认色样
				// RGB_565 每个像素要2字节~不带透明度的颜色
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(new RoundedBitmapDisplayer(10)).build();
	}

	private void circleImageView(String url, ImageView imageView) {
		// 圆形头像
		imageLoader.displayImage(url, imageView, options,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						if (loadedImage != null) {
							((ImageView) view)
									.setScaleType(ImageView.ScaleType.CENTER_CROP);
							boolean firstDisplay = !displayedImages
									.contains(imageUri);
							if (firstDisplay) {
								FadeInBitmapDisplayer.animate((ImageView) view,
										500);
								displayedImages.add(imageUri);
							}
						}
					}

					@Override
					public void onLoadingCancelled(String s, View view) {
					}

					@Override
					public void onLoadingStarted(String s, View view) {
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						Throwable cause = failReason.getCause();
					}
				});
	}
	// ====ImageLoader end====

	
}
